if (!window.llgd) {
	window.llgd = {};
}
//callee 是 arguments 对象的一个属性。它可以用于引用该函数的函数体内当前正在执行的函数。这在函数的名称是未知时很有用，例如在没有名称的函数表达式 (也称为“匿名函数”)内。
//constructor 属性返回对创建此对象的数组函数的引用
//args.swipeDom 大容器对象  args.swipeType 滑动类型  args.swipeDistance 缓冲距离
llgd.iScroll = function(args){
	if (!(this instanceof arguments.callee)) {
		return new arguments.callee(args);
	}
	this.init(args);
}
llgd.iScroll.prototype = {
	constructor:llgd.iScroll,
	init:function(args){
		var that = this;
		//如果传入的对象是一个Dom对象就把他看作是我们的大容器盒子
		if (args.swipeDom && typeof args.swipeDom == 'object') {
			that.parentDom = args.swipeDom;
		}
		//如果不存在父容器就停止初始化
		if(!that.parentDom) return;
		//找到子容器
		that.childDom = that.parentDom.children && that.parentDom.children[0]?that.parentDom.children[0]:"";
		//如果不存在子容器就停止初始化
		if (!that.childDom) {return;}
		//初始化插入的参数
		that.settings = {};
		//默认Y轴滑动 如果不是y的话就以x轴开始滑动
		that.settings.swipeType = args.swipeType?args.swipeType:'y';
		//默认的缓冲滑动距离
		that.settings.swipeDistance = args.swipeDistance>=0?args.swipeDistance:150;
		//初始化滑动
		that._scroll();
	},
	setTranslate:function(translate){
		this.currPosition = translate;
		this._addTransition();
		this._changeTranslate(this.currPosition);
	},
	_addTransition:function(){
		this.childDom.style.transition = "all .2s ease";
		this.childDom.style.webkitTransition - "all .2s ease";
	},
	_removeTransition:function(){
		this.childDom.style.transition = "none";
		this.childDom.style.webkitTransition = "none";
	},
	_changeTranslate:function(translate){
		if (this.settings.swipeType == "y") {
			this.childDom.style.transform = "translateY("+translate+"px)";
			this.childDom.style.webkitTransform = "translateY("+translate+"px)";
		}else{
			this.childDom.style.transform = "translateX("+translate+"px)";
			this.childDom.style.webkitTransform = "translateX("+translate+"px)";
		}
	},
	_scroll:function(){
		var that = this;
		// 滑动类型
		var type = that.settings.swipeType == "y"?true:false;
		var parentHeight = type?that.parentDom.offsetHeight:that.parentDom.offsetWidth;
		var childHeight = type?that.childDom.offsetHeight:that.childDom.offsetWidth;
		//如果子容器没有父容器大时
		if(childHeight < parentHeight){
			if (type) {
				that.childDom.style.height = parentHeight+"px";
				childHeight = parentHeight;
			}else{
				that.childDom.style.width = parentHeight+"px";
				childHeight = parentHeight;
			}
		}
		// 缓冲距离
		var distance = that.settings.swipeDistance;
		// 区间
		// 左侧盒子定位的区间
		that.maxPosition = 0;
		that.minPosition = -(childHeight-parentHeight);
		// 设置滑动的当前位置
		that.currPosition = 0;
		that.startPosition = 0;
		that.endPosition = 0;
		that.movePosition = 0;
		// 滑动
		that.childDom.addEventListener('touchstart', function(e){
			that.startPosition = type?e.touches[0].clientY: e.touches[0].clientX;
		},false);
		that.childDom.addEventListener('touchmove',function(e){
			e.preventDefault();
			// 不停的做滑动的时候记录的endY的值
			that.endPosition = type?e.touches[0].clientY:e.touches[0].clientX;
			that.movePosition = that.startPosition - that.endPosition;

			// 滑动区间
			if((that.currPosition-that.movePosition)<(that.maxPosition+distance) && (that.currPosition-that.movePosition)>(that.minPosition-distance)){
				that._removeTransition();
				that._changeTranslate(that.currPosition-that.movePosition);
			}
		},false);
		window.addEventListener('touchend',function(e){
			if((that.currPosition-that.movePosition)>that.maxPosition){
				that.currPosition = that.maxPosition;
				that._addTransition();
				that._changeTranslate(that.currPosition);
			}else if((that.currPosition-that.movePosition)<that.minPosition){
				that.currPosition = that.minPosition;
				that._addTransition();
				that._changeTranslate(that.currPosition);
			}else{
				that.currPosition = that.currPosition-that.movePosition;
			}
			that._reset();
		},false);

	},
	_reset:function(){
		var that = this;
		that.startPosition = 0;
		that.endPosition = 0;
		that.movePosition = 0;
	}

}